package com.yeqing.iodemo;

import java.io.File;
import java.util.Objects;

public class FileDemo {

    private static void 路径分割符和换行符() {
        System.out.println("展示系统分隔符:" + File.separator);
        System.out.println("展示换行符:\n\ndd");
    }

    private static void File的常用构造方法() {
        File liuBei = new File("/Users/cyq/Develop/三国/刘备.txt");

        File guanYu = new File("/Users/cyq/Develop/三国", "关羽.txt");

        File sanGuo = new File("/Users/cyq/Develop/三国");

        File zhangFei = new File(sanGuo, "张飞.txt");

        File zhaoYun = new File(sanGuo, "赵云.txt");
    }

    private static void 绝对路径和相对路径() {
        File f = new File("/Users/cyq/Develop/gitFile/java-workspace/SpringBoot系列/03-IO操作/NIODemo/src/main/java/com/yeqing/niodemo/Read.java");
        System.out.println("绝对路径:" + f.getAbsolutePath());

        File f2 = new File("FileDemo01.java");
        System.out.println("绝对路径:" + f2.getAbsolutePath());
    }

    private static void 常用方法() {
        File shuiHu = new File("/Users/cyq/Develop/四大名著/水浒传");
        // 不能创建多级目录
        boolean mkdir = shuiHu.mkdir();
        System.out.println("创建多级目录:" + mkdir);
        // 可以创建多级目录，推荐用这个
        boolean mkdirs = shuiHu.mkdirs();
        System.out.println("创建多级目录:" + mkdirs);

        File four = new File("/Users/cyq/Develop/四大名著");
        // 文件夹内不为空，无法删除
        boolean delete = four.delete();
        System.out.println("删除四大名著目录:" + delete);

        File shuiHu2 = new File("/Users/cyq/Develop/四大名著/水浒传");
        // 文件夹内不为空，无法删除
        boolean delete2 = shuiHu2.delete();
        System.out.println("删除水浒传目录:" + delete2);

        File liuBeiDel = new File("/Users/cyq/Develop/三国/刘备.txt");
        boolean delete3 = liuBeiDel.delete();
        System.out.println("删除刘备:" + delete3);
    }

    public static void 文件检测() {
        File sanGuo = new File("/Users/cyq/Develop/三国");
        File zhangFei = new File(sanGuo, "张飞.txt");
        System.out.println("张飞.txt是目录吗？" + zhangFei.isDirectory());
        System.out.println("张飞.txt是文件吗？" + zhangFei.isFile());
        System.out.println("张飞.txt存在吗？" + zhangFei.exists());
        System.out.println("张飞.txt可写吗？" + zhangFei.canWrite());
        System.out.println("张飞.txt可读吗？" + zhangFei.canRead());
        System.out.println("张飞.txt可执行吗？" + zhangFei.canExecute());
        System.out.println("张飞.txt上次修改时间" + zhangFei.lastModified());
    }

    public static void 文件获取() {
        File shiro = new File("shiro");
//        File zhangFei = new File(sanGuo, "张飞.txt");
        System.out.println("绝对路径:" + shiro.getAbsolutePath());
        System.out.println("抽象路径:" + shiro.getPath());
        System.out.println("文件或目录的名称:" + shiro.getName());
        System.out.println("文件的字节数:" + shiro.length());
        for (String s : shiro.list()) {
            System.out.println("文件和目录的名称:" + s);
        }
        // FilenameFilter 返回 true 则添加到 list
        for (File file : Objects.requireNonNull(shiro.listFiles((a, b) -> {
            if (b.contains("pom.xml")) {
                return false;
            } else {
                return true;
            }
        }))) {
            System.out.println("文件和目录的对象:" + file);
        }
    }


    public static void main(String[] args) {
        // ============================== File的常用构造方法 ==============================
//        File的常用构造方法();


        // ============================== 绝对路径和相对路径 ==============================
//        绝对路径和相对路径();

        // ============================== 路径分割符和换行符 ==============================
//        路径分割符和换行符();

        // ============================== File的常用方法 ==============================
//        常用方法();

        // ============================== 文件检测 ==============================
//        文件检测();

        // ============================== 文件获取 ==============================
        文件获取();
    }
}
